package com.rd.p2p.app.additional.lottery;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.LotteryRecordService;
import com.qdlc.p2p.biz.service.lottery.AbstractDrawLotteryService;
import com.qdlc.p2p.biz.service.lottery.LotteryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.biz.zdata.LotteryResult;
import com.qdlc.p2p.common.constant.LotteryCategory;
import com.qdlc.p2p.dal.dto.Lottery;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ScoreLotteryRecordModel;

/**
 * <p>签到抽奖</p>
 * @author Kaiju Zhao 2016年1月14日 下午6:30:31
 * @version V1.0   
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2016年1月14日 下午6:30:31
 * @modify by reason:{原因}
 */
public class AppCountLotteryAction extends BaseAction<ScoreLotteryRecordModel> implements ModelDriven<ScoreLotteryRecordModel> {

    @Resource
    private LotteryRecordService lotteryRecordService;
    @Resource
    private LotteryService lotteryService;

    @Resource
    private AbstractDrawLotteryService countDrawLotteryService;

    private Map<String, Object> data;


    /**
     * <p>进行签到抽奖</p>
     * @author Kaiju Zhao 2016年1月14日 下午6:35:43
     * @throws IOException
     * @modificationHistory=========================逻辑或功能性重大变更记录
     * @modify by user: {修改人} 2016年1月14日 下午6:35:43
     * @modify by reason:{原因}
     */
    @Action("/app/lottery/doSignLottery")
    public void doSignLottery() throws Exception {
        User user = getSessionUser();
        data = new HashMap<String, Object>();
        if (null == user) {
            printWebResCodeAndMsgResult("-1","用户尚未登陆");
            return;
        }
        model.setUser(user);
        // 签到抽奖
        LotteryResult lottery = countDrawLotteryService.process(getSessionUser(), LotteryCategory.SIGN.getCategory());
        // 转盘对应角度设置
        List<Lottery> lotteryConfig = lotteryService
                .getLotteryByCategory(LotteryCategory.SIGN.getCategory());
        if (null == lotteryConfig) {
            printWebResCodeAndMsgResult("0","系统未配置抽奖");
            return;
        }
        int size = lotteryConfig.size();
        int angle = (180 / size) * (lottery.getPos() * 2 - 1);
        lottery.setPos(angle);
        data.put("data", lottery);
        data.put("resCode", 1);
        data.put("resMsg", "进行签到抽奖成功");
        printWebJson(getStringOfJpaObj(data));
        return;
    }

}
